/**
 * Created by USER on 2017/4/22.
 */
$(function(){
    $('#listGrid').treegrid({
        toolbar: '#toolbar',
        title: '组织列表',
        iconCls: 'icon icon-chart_organisation',
        nowrap: false,
        rownumbers: true,
        //animate: true,
        resizable: true,
        fit:true,
        collapsible: false,
        url: '/sys/org/list',
        queryParams:{
            _token : $('meta[name="csrf-token"]').attr('content'),
            curr_menu_id : $('meta[name="curr_menu_id"]').attr('content')
        },
        idField: 'id',
        treeField: 'name',
        columns: [[
            { title: 'id', field: 'id', hidden: true },
            { title: '层级', field: 'menu_level' ,align: 'center'},
            { title: '组织名称', field: 'name' ,align: 'center'},
            { title: '负责人', field: 'leader' ,align: 'center'},
            // { title: '图标', field: 'icon_style', width: 130 },
            { title: '有效', field: 'is_active', width: 50, align: 'center', formatter: format_active}
        ]],
        onClickRow:function (q) {
            $('#click_parent_id').val(q.parent_id);
            $('#click_parent_name').val(q.parent_name);

        }
    });

})

function openAdd(){
    if($('#click_val').val() === ''){
        alert('未选择节点');
    }else{
        $('#form_dialog').dialog({
            title: '添加信息',
            width: 400,
            height: 200,
            closed: false,
            cache: false,
            href: '/sys/org/add',
            modal: true,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: save
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#form_dialog').dialog('close');
                }
            }]
        });
    }

}

function openEdit(){
    var rows = $('#listGrid').treegrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行" , "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑信息',
        width: 400,
        height: 200,
        closed: false,
        cache: false,
        href: '/sys/org/edit?id='+rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save(){
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'/sys/org/save',
        type:'post',
        data:$('#info-form').serialize(),
        dataType:'json',
        success:function (data) {
            if(data.code == 200){
                $('#listGrid').treegrid("reload",{_token : $('meta[name="csrf-token"]').attr('content')});
                $('#form_dialog').dialog('close');
            }else{
                $.messager.alert("提示信息", data.data , "error");
            }
        }
    });
}

/**
 * Name 删除记录
 */
function remove(){

    var items = $('#listGrid').datagrid('getSelected');
    if (items.length === 0) {
        $.messager.alert('警告','请选择一行');
    }else {
        $.messager.confirm('信息提示','确定要删除该记录？', function(result){
            if(result){

                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    type:'post',
                    url:'/sys/org/remove',
                    data:{
                        id:items.id
                    },
                    success:function(data){
                        if(data.code === 200){
                            $.messager.alert('信息提示','删除成功！');
                            $('#listGrid').treegrid("reload",{_token : $('meta[name="csrf-token"]').attr('content')});
                        }
                        else {
                            $.messager.alert('信息提示','删除失败,请稍后再试！');
                            $('#listGrid').treegrid("reload",{_token : $('meta[name="csrf-token"]').attr('content')});
                        }
                    }
                });
            }
        });
    }
}


